<?php class export_salary_m extends MY_Model{
	function __construct(){
		parent::__construct();
		$this->year = date("Y");
		$this->month = date("m");
		$this->room_employ ="";
		$this->name_room  = "";
		$this->list_idroom = array();
		//config salary
		$this->load->model('settings/settings_m');
		$this->config_salary = $this->settings_m->get_config('salary');		
	}
	public function get_levelroom($level,$id_room="",$list_id_room=""){
		if(!empty($id_room))
			$this->db->where("id",$id_room);
		if(!empty($list_id_room) and is_array($list_id_room))
			$this->db->where_in("id",$list_id_room);
		if($level != "")
			$this->db->where("parent_id",$level);
		$this->db->select("title,id")->order_by("id","DESC");
		return $this->db->get("emp_room")->result();
	}
	public function get_list_employ($year,$month,$parent_id,$id_room,$list_room="")
	{
		$this->year   = $year;
		$this->month  = $month;
		$data = $this->get_levelroom($parent_id,$id_room,$list_room); // lay danh sach cac phong;
		$data_employ = "";
		if(!empty($data)){
			$this->get_employ($data);
			foreach($data as $item){
				$this->name_room = $item->title ;
				$data_em = $this->get_rootroom($item->id);		
			}
		}
		return $this->room_employ;
	}
	public function get_employ($data,$name_room="")
	{
		if(!empty($data))
		{
			$i = 0;
			foreach($data as $item)
			{
				$id = $item->id;
				$this->db->select("ser_u.display_name as name_full,ser_g.description as chucvu,sa.*");
				//$this->db->select("ser_u.display_name as name_full");
				$this->db->where("pro.room_id",$item->id);
				$this->db->where("sa.month",$this->month);
				$this->db->join("emp_salary_".$this->year." sa","pro.id = sa.user_id");
				$this->db->join("ser_users ser_u","ser_u.id = pro.id");
				$this->db->join("ser_groups ser_g","ser_u.group_id = ser_g.id");
				$data_employ = $this->db->get("emp_profile pro")->result_array();
				if(!empty($name_room))
				{
					$this->room_employ[$name_room] = array_merge($this->room_employ[$name_room],$data_employ);	
				}
				else
				{
					$this->room_employ[$item->title] = $data_employ;
				}
			}			
	   }
	   return $data;
	}
	public function get_rootroom($id,$data_child="") // lay ra danh sach goc cua phong 
	{
		if(empty($data_child))
		{
			$data_root = $this->get_childroom($id);
			if(!empty($data_root))	
			{
				$this->get_employ($data_root,$this->name_room);
				foreach($data_root as $item)
				{
					$data_child = $this->get_childroom($item->id);
					if(!empty($data_child))
						$this->get_rootroom("",$data_child);
				}
			}
		}
		else
		{
			$this->get_employ($data_child,$this->name_room);
			foreach($data_child as $item)
			{
				$data_child1 = $this->get_childroom($item->id);
				if(!empty($data_child1))
					$this->get_rootroom($id,$data_child1);
			}
		}
		return ;
	}
	public function get_childroom($parent_id)
	{
		if($parent_id != "")
			return $this->db->where("parent_id",$parent_id)->get("emp_room")->result();
		else
			return ;
	}
	public function get_date_work($y,$m){
		$this->db->where('year',$y);
		$this->db->where('month',$m);
		$m = $this->db->get('emp_day')->row();
		return $m;
	}
	public function get_list_idroom($id){
		$this->db->where("parent_id",$id);
		$data = $this->db->get("emp_room")->result();
		array_push($this->list_idroom,$id);
		if(!empty($data))
		foreach($data as $item){
			$this->list_idroom($item->id);
		}
		return $this->list_idroom;
	}
}